/**
 * Created by kenz on 2018/6/4.
 */
import axios from 'axios'
import qs from 'qs'
import { host, signApiKey } from './config'
import { unixTimestamp } from './utils'
import md5 from 'blueimp-md5'


export function httpPost(url, options) {
  let timestamp = unixTimestamp()
  let newData = {
    req: Object.assign({}, options),
    timestamp,
    sign: ''
  }

  if (newData.req) {
    newData.req = JSON.stringify(newData.req)
  }

  let sign = md5(timestamp + signApiKey + newData.req)
  newData.sign = sign


  return new Promise((resolve, reject) => {
    axios.post(host + url, qs.stringify(newData)).then(res => {
      const { data, msg, code } = res.data
      if(code === 0) {
        resolve(data)
      } else {
        reject({code, msg})
      }
    }).catch(err => {
      reject(10009, 'catch error')
    })
  })
}
